<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <script src="./js/vue2.js"></script>
    <script src="./js/vue-router.js"></script>
</head>

<body>
    <div id="app">
        <!-- 添加link-router实现页面跳转 -->
        <router-link to="/index">首页</router-link>
        <router-link to="/product/1">电器</router-link>
        <router-link to="/product/2">水果</router-link>
        <router-link to="/product/3">产品</router-link>
        <!-- 添加路由组件的映射区域 -->
        <router-view></router-view>
    </div>
</body>
<script>
    // 注册组件
    var Index = Vue.component('index', {
        template: "<div>我是首页</div>"
    })
    var Product = Vue.component('product', {
        template: `<div>
                <p>产品</p>
        <p>{{info}}</p>
        </div>
                `,
        data() {
            return {
                info: ''
            }
        },
        watch: {
            '$route'(to, from) {
                var id = to.params.id;
                console.log()
                if (id == 1) {
                    this.info = '这是这是电器页面'
                } else if (id == 2) {
                    this.info = '这是水果 页面'
                } else if (id == 3) {
                    this.info = '这是产品页面'
                }
            }
        },
        mounted() {
            var id = this.$route.params.id;
            if (id == 1) {
                this.info = '这是这是电器页面'
            } else if (id == 2) {
                this.info = '这是水果 页面'
            } else if (id == 3) {
                this.info = '这是产品页面'
            }
        },

    })
    var router = new VueRouter({
        routes: [
            { name: 'Index', path: '/index', component: Index },
            { name: 'Login', path: '/product/:id', component: Product}
        ]
    })
    var vm = new Vue({
        el: '#app',
        router: router,
        data: {

        }
    })
</script>

</html>